<?php

/**
 * Ventas filter form.
 *
 * @package    tercermilenio
 * @subpackage filter
 * @author     Your name here
 */
class VentasFormFilter extends BaseVentasFormFilter {

    public function configure() {

        $this->widgetSchema['vta_fecha'] = new sfWidgetFormFilterDate(
                        array(
                            'from_date' => new sfWidgetFormJQueryDate(array('format' => '%day%/%month%/%year%')),
                            'to_date' => new sfWidgetFormJQueryDate(array('format' => '%day%/%month%/%year%')),
                            'with_empty' => false,
                ));



        $this->widgetSchema['vta_cliente']->setOption('add_empty', 'Seleccione un cliente');
        $this->widgetSchema['vta_usuario']->setOption('add_empty', 'Seleccione un usuario');
        $this->widgetSchema['vta_tipo']->setOption('add_empty', 'Seleccione un tipo de vta');

        $this->widgetSchema['vta_fecha']->addOption('template', 'Desde %from_date%<br/>Hasta %to_date%');
        //var_dump($this->widgetSchema['vta_fecha']->getOption('to_date')->setDefault());exit();//['from_date']->addOption('culture', 'es');
    }

    public function setup() {

        parent::setup();

        $this->setWidgets(array(
            'vta_nro_factura' => new sfWidgetFormFilterInput(array('with_empty' => false)),
            'vta_cliente' => new sfWidgetFormPropelChoice(array('model' => 'Clientes')),
            'vta_tipo' => new sfWidgetFormPropelChoice(array('model' => 'TipoVentas', 'add_empty' => true)),
            /*'vta_fecha' => new sfWidgetFormFilterDate(array(
                'from_date' => new sfWidgetFormDateJQueryUI(array(), array()),
                'to_date' => new sfWidgetFormDateJQueryUI(array('culture' => "es")),
                'with_empty' => false)),*/
            //'vta_tipo' => new sfWidgetFormFilterInput(),
            //'vta_total' => new sfWidgetFormFilterInput(array('with_empty' => false)),
            'vta_usuario' => new sfWidgetFormPropelChoice(array('model' => 'sfGuardUser')),
                //'devoluciones_list' => new sfWidgetFormPropelChoice(array('model' => 'Productos', 'add_empty' => true)),
                //'detalle_ventas_list' => new sfWidgetFormPropelChoice(array('model' => 'Productos', 'add_empty' => true)),
        ));

        $this->setValidators(array(
            'vta_nro_factura' => new sfValidatorSchemaFilter('text', new sfValidatorInteger(array('required' => false))),
            'vta_cliente' => new sfValidatorPropelChoice(array('required' => false, 'model' => 'Clientes', 'column' => 'cli_id')),
            'vta_tipo' => new sfValidatorPropelChoice(array('required' => false, 'model' => 'TipoVentas', 'column' => 'tip_vta_id')),
            'vta_fecha' => new sfValidatorDateRange(array('required' => false, 'from_date' => new sfValidatorDate(array('required' => false)), 'to_date' => new sfValidatorDate(array('required' => false)))),
            //'vta_tipo' => new sfValidatorPass(array('required' => false)),
            //'vta_total' => new sfValidatorSchemaFilter('text', new sfValidatorNumber(array('required' => false))),
            'vta_usuario' => new sfValidatorPropelChoice(array('required' => false, 'model' => 'Usuarios', 'column' => 'usu_id')),
                //'devoluciones_list' => new sfValidatorPropelChoice(array('model' => 'Productos', 'required' => false)),
                //'detalle_ventas_list' => new sfValidatorPropelChoice(array('model' => 'Productos', 'required' => false)),
        ));

        $this->widgetSchema->setNameFormat('ventas_filters[%s]');

        $this->errorSchema = new sfValidatorErrorSchema($this->validatorSchema);
    }

    public function addVtaFechaColumnCriteria(Criteria $criteria, $field, $values) {
            $inicioDia = '00:00:00';
            $finDia = '23:59:59';
            
        if (!empty($values['from'])) {
            $filterInicio = $values['from'] . ' ' . $inicioDia;
            $criteria->add(VentasPeer::VTA_FECHA, $values['from'], Criteria::GREATER_EQUAL);
        }
        
        if (!empty($values['from']) && !empty($values['to'])) {
            $filterFin = $values['to'] . ' ' . $finDia;
            $criteria->add(VentasPeer::VTA_FECHA, $values['to'], Criteria::LESS_EQUAL);
        }
    }

}
